package com.xzsoft.gdwz.services.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xzsoft.gdwz.common.CommonUtil;
import com.xzsoft.gdwz.dao.OrderDao;
import com.xzsoft.gdwz.model.OrderHeader;
import com.xzsoft.gdwz.model.OrderLine;
import com.xzsoft.gdwz.services.SnOrderService;

@Transactional
@Service("snOrderService")
public class SnOrderServiceImpl implements SnOrderService {
	
	Logger logger = Logger.getLogger(SnOrderServiceImpl.class);
	
	@Autowired
	private OrderDao orderDao;
	@Autowired
	private CommonUtil commonUtil;
	@Override
	@Transactional(propagation = Propagation.REQUIRED, noRollbackFor = RuntimeException.class)
	public void saveOrder(JSONObject map) throws Exception {
			OrderHeader oh = new OrderHeader();
			oh.setEcommerceName("SN");
			oh.setEcommerceOrderId((String)map.get("orderId"));
			oh.setInqHid(UUID.randomUUID().toString());
			oh.setInqHCode((String)map.get("orderId"));
			oh.setInqAmount(map.get("orderAmt")+"");
			oh.setCreateDate((String)map.get("createTime"));
			oh.setCreateBy((String)map.get("accountName"));
			oh.setCreateByPhone((String)map.get("receiverTel"));
			oh.setAddress((String)map.get("receiverAddress"));
			oh.setVendorName("苏宁");
			oh.setVendorPhone("");
			oh = commonUtil.initUserOrgMsg(oh);
			//订单明细信息
			JSONArray array = (JSONArray)map.get("orderItemList");
			//设置订单名称，第一行物资信息+项目单位简称
			if(array.size()>0){
				oh.setInqHName((String)((JSONObject)array.get(0)).get("commdtyName")+"("+oh.getComName()+")-苏宁采购");
				oh.setReqDate( (String)((JSONObject)array.get(0)).get("hopeArriveTime"));
			}
			orderDao.insertOrderHeader(oh);
			JSONObject jo =null;
			for(int i = 0;i<array.size();i++){
				OrderLine ol = new OrderLine();
				jo =array.getJSONObject(i);
				ol.setMtName((String) jo.get("commdtyName"));
				ol.setEcommerceName("SN");
				ol.setEcommerceSkuId((String)jo.get("orderId"));
				ol.setInqHid(oh.getInqHid());
				ol.setCcidId(UUID.randomUUID().toString());
				ol.setReqQuantity(jo.get("skuNum")+"");
				ol.setInqPrice(jo.get("unitPrice")+"");
				ol.setInqAmount(jo.get("skuAmt")+"");
				ol.setCreationDate((String)map.get("createTime"));
				ol.setCreatedBy((String)map.get("accountName"));
				ol.setReqDate((String)map.get("hopeArriveTime"));
				ol = commonUtil.initUserOrgMsg(ol);
				orderDao.insertOrderLine(ol);
				ol = null;
			}
		}
	/**
	 * 获取订单信息
	 */
	@Override
	public List<OrderHeader> getOrder() {
		OrderHeader oh = new OrderHeader();
		List<OrderHeader> orderlist =  new ArrayList<OrderHeader>();
		orderlist = orderDao.SelectOrder();
		return orderlist;
	}
	/**
	 * 回写订单is_update字段
	 */
	@Override
	public void UpdateOrderStatus(String inqid) {
		orderDao.UpdateOrder(inqid); 
	}
}